package cn.dubito.miaosha.controller;

import cn.dubito.miaosha.controller.vo.CodeMsg;
import cn.dubito.miaosha.controller.vo.Result;
import cn.dubito.miaosha.domain.OrderInfo;
import cn.dubito.miaosha.domain.SeckillUser;
import cn.dubito.miaosha.service.GoodsService;
import cn.dubito.miaosha.service.OrderService;
import cn.dubito.miaosha.vo.GoodsVO;
import cn.dubito.miaosha.vo.OrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author liliang
 * @description:
 * @date 2018/4/4.
 */
@RequestMapping("order")
@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("detail")
    @ResponseBody
    public Result<OrderDetailVO> detail(SeckillUser user, @RequestParam("orderId") long orderId) {
        if (user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo orderInfo = orderService.getOrderById(orderId);
        if (orderInfo == null) {
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }

        Long goodsId = orderInfo.getGoodsId();
        GoodsVO goodsVO = goodsService.getGoodsDetailByGoodsId(goodsId);

        OrderDetailVO detailVO = new OrderDetailVO();
        detailVO.setGoods(goodsVO);
        detailVO.setOrder(orderInfo);

        return Result.success(detailVO);
    }

}
